/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package jsf.generator.translator;

import com.google.gson.Gson;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Locale;
import jsf.generator.translator.models.Item;
import jsf.generator.translator.models.Items;

/**
 *
 * @author abosancic
 */
public class Translate {

    public Translate() {
    }
    
//    public static Properties translate(String value, Locale from, Locale to)
//    {
//        Properties p = new Properties();
//
//        try {
//            String charset = "UTF-8";
//            URL url = new URL("http://frengly.com/controller?action=translateAnyAny&rest=1&s_langs="
//                    + from.getLanguage() + "&lb_langs=" + to.getLanguage()
//                    + "&text=" + URLEncoder.encode(value, charset));
//
//            BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream(), charset));
//            String readLine = "";
//
//            StringBuffer responseBuffer = new StringBuffer();
//            while ((readLine = reader.readLine()) != null) {
//                responseBuffer.append(new String(readLine.getBytes(), charset));
//            }
//
//            Item item = new Gson().fromJson(responseBuffer.toString(), Item.class);
//            String translated = "";
//
//            if (item != null) {
//                for (Items items : item.list) {
//                    translated += items.destWord;
//                }
//            }
//
//            p.put(value.trim().replaceAll("\\s+", ""), translated);
//
//        } catch (Exception e) {
//            System.out.println(e.toString());
//        }
//
//        return p;
//    }
    
    public static String translate(String value, Locale from, Locale to)
    {

        String translated = "";
        
        try {
            String charset = "UTF-8";
            URL url = new URL("http://frengly.com/controller?action=translateAnyAny&rest=1&s_langs="
                    + from.getLanguage() + "&lb_langs=" + to.getLanguage()
                    + "&text=" + URLEncoder.encode(value, charset));

            BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream(), charset));
            String readLine = "";

            StringBuffer responseBuffer = new StringBuffer();
            while ((readLine = reader.readLine()) != null) {
                responseBuffer.append(new String(readLine.getBytes(), charset));
            }

            Item item = new Gson().fromJson(responseBuffer.toString(), Item.class);

            if (item != null) {
                for (Items items : item.list) {
                    translated += StringUtils.native2Ascii(items.destWord);
                }
            }

        } catch (Exception e) {
            System.out.println(e.toString());
        }

        return translated;
    }
    
}
